//
//  registView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/13.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "registView.h"

@interface registView ()
{
    CGFloat viewHeight;
    CGFloat increaseHeight;
}

@property (nonatomic, strong) UITextField *userName;
@property (nonatomic, strong) UITextField *serverIP;
@property (nonatomic, strong) UITextField *port;
@property (nonatomic, strong) UITextField *serverID;

@end

@implementation registView

- (void)addBackGround
{
    UIImage *backImage = GET_IAMGE_WITH_NAME(@"Image/registView/register_background");
    UIImageView *imageView = [[UIImageView alloc] initWithImage:backImage];
    [imageView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view addSubview:imageView];
    imageView.userInteractionEnabled = YES;
    
    UIImage *bg = GET_IAMGE_WITH_NAME(@"Image/registView/register_bg");
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bg];
    [bgImageView setFrame:CGRectMake(0, 0, self.view.bounds.size.width - 50, self.view.bounds.size.height - 100)];
    bgImageView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    [imageView addSubview:bgImageView];
    viewHeight = bgImageView.bounds.size.height;
    [self addRegisterButtonWithView:bgImageView];
    [self addTitleLabelWithView:bgImageView];
    [self addInputBackGroundWithView:bgImageView];
    bgImageView.userInteractionEnabled = YES;
}

- (void)addRegisterButtonWithView:(UIView *)view
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, view.bounds.size.height * 0.8635 - 10,
                                                                  self.view.bounds.size.width - 90, viewHeight * 0.1)];
    UIImage *background = GET_IAMGE_WITH_NAME(@"Image/startView/main_button_n");
    [button setBackgroundImage:background forState:UIControlStateNormal];
    [button setTitle:@"注册" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:22];
    [button addTarget:self action:@selector(pressRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}

- (void)addTitleLabelWithView:(UIView *)view
{
    UILabel *titileLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    titileLabel.center = CGPointMake(view.bounds.size.width / 2, viewHeight * 0.224);
    titileLabel.text = @"注册";
    titileLabel.textAlignment = NSTextAlignmentCenter;
    titileLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:25];
    titileLabel.textColor = [UIColor blackColor];
    [view addSubview:titileLabel];
    titileLabel = nil;
    UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 230, 30)];
    titleLabel2.center = CGPointMake(view.bounds.size.width / 2, viewHeight * 0.3);
    titleLabel2.text = @"请填写相关信息";
    titleLabel2.textAlignment = NSTextAlignmentCenter;
    titleLabel2.font = [UIFont fontWithName:@"Arial-BoldMT" size:22];
    titleLabel2.textColor = [UIColor grayColor];
    [view addSubview:titleLabel2];
}

- (void)addInputBackGroundWithView:(UIView *)view
{
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/registView/register_input_bg");
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, self.view.bounds.size.width - 90, viewHeight * 0.44)];
    imageView.center = CGPointMake(view.bounds.size.width / 2, viewHeight / 2 + 30);
    
    for (int i = 1; i < 4; i++) {
        [self addLineWithView:imageView andNumber:i];
    }
    
    [imageView addSubview:self.userName];
    [imageView addSubview:self.serverIP];
    [imageView addSubview:self.port];
    [imageView addSubview:self.serverID];;
    imageView.userInteractionEnabled = YES;
    [view addSubview:imageView];
}

- (void)addLineWithView:(UIView *)view andNumber:(int)num
{
    UIImage *line = GET_IAMGE_WITH_NAME(@"Image/registView/register_editview_line");
    UIImageView *imageView = [[UIImageView alloc] initWithImage:line];
    [imageView setFrame:CGRectMake(0, viewHeight * 0.11 * num, self.view.bounds.size.width - 90, 1)];
    
    [view addSubview:imageView];
}

- (UITextField *)userName
{
    if(!_userName){
        _userName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 90, viewHeight * 0.11 - 1)];
        
        UIImage *iamge = GET_IAMGE_WITH_NAME(@"Image/registView/icon_name");
        UIImageView *imageView = [[UIImageView alloc] initWithImage:iamge];
        [imageView setFrame:CGRectMake(0, 0, viewHeight * 0.11 - 10, viewHeight * 0.11 - 10)];
        _userName.leftView = imageView;
        _userName.leftViewMode = UITextFieldViewModeAlways;
        
        _userName.placeholder = @"昵称";    //默认文字
        _userName.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    }
    return _userName;
}

- (UITextField *)serverIP
{
    if(!_serverIP){
        _serverIP = [[UITextField alloc] initWithFrame:CGRectMake(0, viewHeight * 0.11 , self.view.bounds.size.width - 90, viewHeight * 0.11 - 1)];
        
        UIImage *iamge = GET_IAMGE_WITH_NAME(@"Image/registView/icon_ip");
        UIImageView *imageView = [[UIImageView alloc] initWithImage:iamge];
        [imageView setFrame:CGRectMake(0, 0, viewHeight * 0.11 - 10, viewHeight * 0.11 - 10)];
        _serverIP.leftView = imageView;
        _serverIP.leftViewMode = UITextFieldViewModeAlways;
        
        _serverIP.placeholder = @"主机IP";    //默认文字
        _serverIP.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    }
    return _serverIP;
}

- (UITextField *)port
{
    if(!_port){
        _port = [[UITextField alloc] initWithFrame:CGRectMake(0, viewHeight * 0.22, self.view.bounds.size.width - 90, viewHeight * 0.11 - 1)];
        
        UIImage *iamge = GET_IAMGE_WITH_NAME(@"Image/registView/icon_port_number");
        UIImageView *imageView = [[UIImageView alloc] initWithImage:iamge];
        [imageView setFrame:CGRectMake(0, 0, viewHeight * 0.11 - 10, viewHeight * 0.11 - 10)];
        _port.leftView = imageView;
        _port.leftViewMode = UITextFieldViewModeAlways;
        
        _port.placeholder = @"端口号";    //默认文字
        _port.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    }
    return _port;
}

- (UITextField *)serverID
{
    if(!_serverID){
        _serverID = [[UITextField alloc] initWithFrame:CGRectMake(0, viewHeight * 0.33, self.view.bounds.size.width - 90, viewHeight * 0.11 - 1)];
        
        UIImage *iamge = GET_IAMGE_WITH_NAME(@"Image/registView/icon_id");
        UIImageView *imageView = [[UIImageView alloc] initWithImage:iamge];
        [imageView setFrame:CGRectMake(0, 0, viewHeight * 0.11 - 10, viewHeight * 0.11 - 10)];
        _serverID.leftView = imageView;
        _serverID.leftViewMode = UITextFieldViewModeAlways;
        
        _serverID.placeholder = @"主机ID";    //默认文字
        _serverID.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    }
    return _serverID;
}

//按钮响应函数
- (void)pressRegisterButton
{
    if(![self.userName.text isEqualToString:@""] &&
       ![self.serverIP.text isEqualToString:@""] &&
       ![self.port.text isEqualToString:@""] &&
       ![self.serverID.text isEqualToString:@""]){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self.delegate newUserOfName:self.userName.text
                                  andKey:@"123456789"
                                serverIP:self.serverIP.text
                                    port:[self.port.text intValue]
                                serverId:self.serverID.text];
        });
    }
    [self.delegate modalViewControllerDidClickedDismissButton:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addBackGround];
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    //注册键盘隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //解除键盘出现的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    //解除键盘隐藏的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //获得键盘尺寸
    NSDictionary *info = [notif userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    increaseHeight = self.view.bounds.size.height * 0.44 + 245 - (self.view.bounds.size.height - keyboardSize.height);
    if(increaseHeight > 0){
        self.view.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -increaseHeight);
    }
}

- (void)keyboardWillHide:(NSNotification *)notif
{
    self.view.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
